package com.lzq.model.responsibility;

/**
 * @author : modify
 * @version : v1.0
 * @description :
 * @createTime : 2022/11/28 10:58
 * @updateUser : modify
 * @updateTime : 2022/11/28 10:58
 * @updateRemark : 责任链模式
 */
public class LeaveRequest {
    private String name;
    private int num;
    private String content;

    public LeaveRequest(String name, int num, String content) {
        this.name = name;
        this.num = num;
        this.content = content;
    }

    public String getName() {
        return name;
    }

    public int getNum() {
        return num;
    }

    public String getContent() {
        return content;
    }
}

/**
 * 抽象处理者类
 */
abstract class Handler {
    protected final static int NUM_ONE = 1;
    protected final static int NUM_THREE = 3;
    protected final static int NUM_SEVEN = 7;

    private int numStart;
    private int numEnd;

    private Handler nextHandler;

    public Handler(int numStart) {
        this.numStart = numStart;
    }

    public Handler(int numStart, int numEnd) {
        this.numStart = numStart;
        this.numEnd = numEnd;
    }

    public void setNextHandler(Handler nextHandler) {
        this.nextHandler = nextHandler;
    }

    public final void submit(LeaveRequest leave) {
        if (0 == this.numStart) {
            return;
        }
        //如果请假天数达到该领导者的处理要求
        if (leave.getNum() >= this.numStart) {
            this.handleLeave(leave);
            //如果还有上级 并且请假天数超过了当前领导的处理范围
            if (null != this.nextHandler && leave.getNum() > this.numEnd) {
                this.nextHandler.submit(leave);
            } else {
                System.out.println("流程结束");
            }
        }
    }

    /**
     * 请加流程
     *
     * @param leave
     */
    protected abstract void handleLeave(LeaveRequest leave);
}

class GroupLeader extends Handler {
    public GroupLeader() {
        super(NUM_ONE, NUM_THREE);
    }

    @Override
    protected void handleLeave(LeaveRequest leave) {
        System.out.println("GroupLeader ===>>>> name : " + leave.getName() + "  ,num : " + leave.getNum() + " , content: " + leave.getContent());
    }
}

class Manager extends Handler {

    public Manager() {
        super(NUM_THREE, NUM_SEVEN);
    }

    @Override
    protected void handleLeave(LeaveRequest leave) {
        System.out.println("Manager ===>>>> name : " + leave.getName() + "  ,num : " + leave.getNum() + " , content: " + leave.getContent());
    }
}

class GeneralManager extends Handler {

    public GeneralManager() {
        super(NUM_SEVEN);
    }

    @Override
    protected void handleLeave(LeaveRequest leave) {
        System.out.println("GeneralManager  ===>>>> name : " + leave.getName() + "  ,num : " + leave.getNum() + " , content: " + leave.getContent());
    }
}

class Test {
    public static void main(String[] args) {
        LeaveRequest leaveRequest = new LeaveRequest("月月鸟", 1, "不想上班");
        GroupLeader groupLeader = new GroupLeader();
        Manager manager = new Manager();
        GeneralManager generalManager = new GeneralManager();
        groupLeader.setNextHandler(manager);
        manager.setNextHandler(generalManager);

        groupLeader.submit(leaveRequest);
    }
}